package com.chaos.eurekaproducer.utils;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author liaopeng
 * @title: MyThreadPoolUtils
 * @projectName eureka-producer
 * @description: 自定义线程池
 * @date 2021/9/27下午4:20
 */
public class MyThreadPoolUtils {

    /**
     * 核心线程数
     */
//    private static final int corePoolSize=Runtime.getRuntime().availableProcessors();
    private static final int corePoolSize=2;

    /**
     * 最大线程数
     */
//    private static final int maxPoolSize=corePoolSize*2;
    private static final int maxPoolSize=10;

    /**
     * 空闲线程存活时间
     */
    private static final int keepAliveTime=60;

    /**
     * 任务队列大小
     */
    private static final int blockingQueueSize=10;

    private static ThreadPoolExecutor executor;
    static {
        if (executor==null){
            executor = new ThreadPoolExecutor(corePoolSize,maxPoolSize,keepAliveTime, TimeUnit.SECONDS,new LinkedBlockingQueue<>(),new MyThreadFactory(),new MyRejectionHandler());
        }
    }

    public static class MyThreadFactory implements ThreadFactory{

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread();
            return thread;
        }
    }
}
